//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackScholesCalculator.h"
using namespace Cephei::QL::Pricingengines;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Pricingengines/BlackCalculator.h>
using namespace Cephei::QL::Instruments;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Double spot, Double growth, Double stdDev, Double discount) : CBlackCalculator(CBlackScholesCalculator::typeid)
{
    CStrikedTypePayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phBlackScholesCalculator = NULL;
#endif
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        QuantLib::Real _spot = (QuantLib::Real)ValueHelper::Convert (spot);
        QuantLib::DiscountFactor _growth = (QuantLib::DiscountFactor)ValueHelper::Convert (growth);
        QuantLib::Real _stdDev = (QuantLib::Real)ValueHelper::Convert (stdDev);
        QuantLib::DiscountFactor _discount = (QuantLib::DiscountFactor)ValueHelper::Convert (discount);
        _ppBlackScholesCalculator = new boost::shared_ptr<QuantLib::BlackScholesCalculator> (new QuantLib::BlackScholesCalculator ( _payoff,  _spot,  _growth,  _stdDev,  _discount ));
        SetBlackCalculator (boost::dynamic_pointer_cast<QuantLib::BlackCalculator> (*_ppBlackScholesCalculator));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (boost::shared_ptr<QuantLib::BlackScholesCalculator>& childNative, Object^ owner) : CBlackCalculator(CBlackScholesCalculator::typeid)
{
#ifdef HANDLE
	_phBlackScholesCalculator = NULL;
#endif
	_ppBlackScholesCalculator = &childNative;
    _ppBlackCalculator = new boost::shared_ptr<QuantLib::BlackCalculator> (boost::dynamic_pointer_cast<QuantLib::BlackCalculator> (*_ppBlackScholesCalculator));
}
Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (QuantLib::BlackScholesCalculator& childNative, Object^ owner) : CBlackCalculator(CBlackScholesCalculator::typeid)
{
#ifdef HANDLE
	_phBlackScholesCalculator = NULL;
#endif
	_ppBlackScholesCalculator = new boost::shared_ptr<QuantLib::BlackScholesCalculator> (&childNative);
    _ppBlackCalculator = new boost::shared_ptr<QuantLib::BlackCalculator> (boost::dynamic_pointer_cast<QuantLib::BlackCalculator> (*_ppBlackScholesCalculator));
    _BlackScholesCalculatorOwner = owner;
    _BlackCalculatorOwner = owner;
}

Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (CBlackScholesCalculator^ copy) : CBlackCalculator(CBlackScholesCalculator::typeid)
{
#ifdef HANDLE
	_phBlackScholesCalculator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackScholesCalculator = new boost::shared_ptr<QuantLib::BlackScholesCalculator> (copy->GetShared());
        _ppBlackCalculator = new boost::shared_ptr<QuantLib::BlackCalculator> (boost::dynamic_pointer_cast<QuantLib::BlackCalculator> (*_ppBlackScholesCalculator));
    }
}
Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (System::Type^ t) : CBlackCalculator(CBlackScholesCalculator::typeid)
{
#ifdef HANDLE
	_phBlackScholesCalculator = NULL;
#endif
	if (!t->IsSubclassOf(CBlackScholesCalculator::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (QuantLib::Handle<QuantLib::BlackScholesCalculator>& childNative, Object^ owner)  : CBlackCalculator(CBlackScholesCalculator::typeid)
{
	_phBlackScholesCalculator = &childNative;
	_ppBlackScholesCalculator = &static_cast<boost::shared_ptr<QuantLib::BlackScholesCalculator>>(childNative.currentLink());
    _ppBlackCalculator = new boost::shared_ptr<QuantLib::BlackCalculator> (boost::dynamic_pointer_cast<QuantLib::BlackCalculator> (*_ppBlackScholesCalculator));
    _BlackScholesCalculatorOwner = owner;
}
Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (QuantLib::Handle<QuantLib::BlackScholesCalculator> childNative)  : CBlackCalculator(CBlackScholesCalculator::typeid)
{
	_phBlackScholesCalculator = &childNative;
	_ppBlackScholesCalculator = &static_cast<boost::shared_ptr<QuantLib::BlackScholesCalculator>>(childNative.currentLink());
    _ppBlackCalculator = new boost::shared_ptr<QuantLib::BlackCalculator> (boost::dynamic_pointer_cast<QuantLib::BlackCalculator> (*_ppBlackScholesCalculator));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::CBlackScholesCalculator::CBlackScholesCalculator (QuantLib::BlackScholesCalculator childNative)  : CBlackCalculator(CBlackScholesCalculator::typeid)
{
#ifdef HANDLE
	_phBlackScholesCalculator = NULL;
#endif
	_ppBlackScholesCalculator = new boost::shared_ptr<QuantLib::BlackScholesCalculator> (new QuantLib::BlackScholesCalculator (childNative));
    _ppBlackCalculator = new boost::shared_ptr<QuantLib::BlackCalculator> (boost::dynamic_pointer_cast<QuantLib::BlackCalculator> (*_ppBlackScholesCalculator));
}
#endif

Cephei::QL::Pricingengines::CBlackScholesCalculator::~CBlackScholesCalculator ()
{
    if (_ppBlackScholesCalculator != NULL)
    {
	    delete _ppBlackScholesCalculator;
        _ppBlackScholesCalculator = NULL;
    }
}
Cephei::QL::Pricingengines::CBlackScholesCalculator::!CBlackScholesCalculator ()
{
    if (_ppBlackScholesCalculator != NULL)
    {
	    delete _ppBlackScholesCalculator;
    }
}
QuantLib::BlackScholesCalculator& Cephei::QL::Pricingengines::CBlackScholesCalculator::GetReference ()
{
    if (_ppBlackScholesCalculator == NULL) throw gcnew NativeNullException ();
	return **_ppBlackScholesCalculator;
}
boost::shared_ptr<QuantLib::BlackScholesCalculator>& Cephei::QL::Pricingengines::CBlackScholesCalculator::GetShared ()
{
    if (_ppBlackScholesCalculator == NULL) throw gcnew NativeNullException ();
	return *_ppBlackScholesCalculator;
}
QuantLib::BlackScholesCalculator* Cephei::QL::Pricingengines::CBlackScholesCalculator::GetPointer ()
{
    if (_ppBlackScholesCalculator == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackScholesCalculator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackScholesCalculator>& Cephei::QL::Pricingengines::CBlackScholesCalculator::GetHandle ()
{
	if (_phBlackScholesCalculator == NULL)
	{
		_phBlackScholesCalculator = new Handle<QuantLib::BlackScholesCalculator> (*_ppBlackScholesCalculator);
	}
	return *_phBlackScholesCalculator;
}
#endif
bool Cephei::QL::Pricingengines::CBlackScholesCalculator::HasNative () 
{
	return (_ppBlackScholesCalculator != NULL);
}

Double Cephei::QL::Pricingengines::CBlackScholesCalculator::Delta ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackScholesCalculator)->delta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::CBlackScholesCalculator::Elasticity ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackScholesCalculator)->elasticity ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::CBlackScholesCalculator::Gamma ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackScholesCalculator)->gamma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::CBlackScholesCalculator::Theta (Double maturity)
{
    try
    {
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackScholesCalculator)->theta ( _maturity );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::CBlackScholesCalculator::ThetaPerDay (Double maturity)
{
    try
    {
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackScholesCalculator)->thetaPerDay ( _maturity );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::IBlackScholesCalculator^ Cephei::QL::Pricingengines::CBlackScholesCalculator_Factory::Create (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Double spot, Double growth, Double stdDev, Double discount)
{
    return gcnew CBlackScholesCalculator ( payoff,  spot,  growth,  stdDev,  discount);
}
